package com.hnhcyy.goods.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hnhcyy.goods.pojo.GoodsExtend;
import com.hnhcyy.goods.pojo.SpMchk;
import com.hnhcyy.goods.pojo.SpOldMchk;
import com.hnhcyy.goods.service.GoodsRangeService;
import org.apache.commons.codec.binary.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 商品控销接口
 * @author sheyongjun  
 * @date 2019年2月26日
 */
@RestController
@RequestMapping("/range")
public class GoodsRangeApi {

	@Autowired
	private GoodsRangeService goodsRangeService;

	private final String lsxNumber = "DBM00000049"; // 乐赛仙
	private final String zatNumber = "DBM00000010"; // 佐安堂
	private final String cxNumber = "DBM00000039"; // 初心
	
	/**
	 * 老客户
	 * @author sheyongjun
	 * @date 2019年2月26日
	 */
	@PostMapping("/oldCust")
	public boolean oldCustRange(String text){
		// 解析json
		JSONObject jsonObject = JSONObject.parseObject(text);
		String dwbh = jsonObject.getString("FCustNumber");
		JSONArray array = jsonObject.getJSONArray("FEntry");
		// 转成对象list
		List<SpOldMchk> spOldMchkList = new ArrayList<>();
		for(int i = 0; i < array.size(); i++) {
			SpOldMchk spOldMchk = new SpOldMchk();
			spOldMchk.setDwbh(dwbh);
			spOldMchk.setSpid(array.getJSONObject(i).getString("FMatNumber"));
			spOldMchkList.add(spOldMchk);
		}
		return goodsRangeService.oldCustRange(spOldMchkList);
	} 

	/**
	 * 客户加减
	 * @author sheyongjun
	 * @date 2019年2月26日
	 */
	@PostMapping("/custAdjust")
	public boolean custAdjustRange(String text){
		// 解析json
		JSONObject jsonObject = JSONObject.parseObject(text);
		String dwbh = jsonObject.getString("FCustNumber");
		JSONArray array = jsonObject.getJSONArray("FEntry");
		// 转成对象list
		List<SpMchk> spMchkList = new ArrayList<>();
		for(int i = 0; i < array.size(); i++) {
			SpMchk spMchk = new SpMchk();
			spMchk.setDwbh(dwbh);
			spMchk.setSpid(array.getJSONObject(i).getString("FMatNumber"));
			int isMarketAble = array.getJSONObject(i).getIntValue("FIsMarketAble");
			spMchk.setFenable(isMarketAble > 0);
			spMchkList.add(spMchk);
		}
		return goodsRangeService.custAdjustRange(spMchkList);
	} 

	/**
	 * 省区控销（禁销）
	 * @author sheyongjun
	 * @date 2019年2月26日
	 */
	@PostMapping("/provinceRange")
	public boolean provinceRange(String text) {
		// 解析json
		JSONObject jsonObject = JSONObject.parseObject(text);
		String spid = jsonObject.getString("FMatNumber");
		JSONArray entryArray = jsonObject.getJSONArray("FEntry");
		// 转成省区编码数组
		// 乐赛仙 
		List<Integer> lsxProvinceCodeList = new ArrayList<>();
		// 佐安堂
		List<Integer> zatProvinceCodeList = new ArrayList<>();
		// 初心
		List<Integer> cxProvinceCodeList = new ArrayList<>();

		for(int i = 0; i< entryArray.size(); i++) {
			// 省份编码
			int provinceCode = entryArray.getJSONObject(i).getInteger("FProvinceCode");
			// 部门编码
			String deptNumber = entryArray.getJSONObject(i).getString("FDeptNumber");
			if(StringUtils.equals(deptNumber, lsxNumber)) {
				// 乐赛仙
				lsxProvinceCodeList.add(provinceCode);
			}else if(StringUtils.equals(deptNumber, zatNumber)) {
				// 佐安堂
				zatProvinceCodeList.add(provinceCode);
			}else {
				// 初心
				cxProvinceCodeList.add(provinceCode);
			}
		}
		Map<String, List<Integer>> map  = new HashMap<>();
		if(!lsxProvinceCodeList.isEmpty()) {
			map.put(lsxNumber, lsxProvinceCodeList);
		}
		if(!zatProvinceCodeList.isEmpty()) {
			map.put(zatNumber, zatProvinceCodeList);
		}
		if(!cxProvinceCodeList.isEmpty()) {
			map.put(cxNumber, cxProvinceCodeList);
		}
		System.out.println(map);
		for(Entry<String, List<Integer>> entry : map.entrySet()) {
			String key = entry.getKey();
			int sjid = 1;
			if(StringUtils.equals(key, lsxNumber)) {
				sjid = 1;
			}else if(StringUtils.equals(key, zatNumber)) {
				sjid = 2;
			}else {
				sjid = 3;
			}
			List<Integer> provinceCodeList = entry.getValue();
			StringBuilder xsqy = new StringBuilder();
			for(int i = 1; i < 35; i++) {
				if(provinceCodeList.contains(i)) {
					xsqy.append("0");
				}else {
					xsqy.append("1");
				}
			}
			System.out.println(key + "-------" + xsqy);
			GoodsExtend goodsExtend = new GoodsExtend();
			goodsExtend.setSjid(sjid);
			goodsExtend.setSpid(spid);
			goodsExtend.setXsqy(xsqy.toString());
			boolean flag = goodsRangeService.provinceRange(goodsExtend);
			if(!flag) {
				return false;
			}
		}
		return true;
	}

	/*public static void main(String[] args) {
		String text = "{\"FMatNumber\":\"SPH00000001\",\"FEntry\":[{\"FDeptNumber\":\"DBM00000049\",\"FProvinceCode\":\"6\"},"
				+ "{\"FDeptNumber\":\"DBM00000049\",\"FProvinceCode\":\"7\"},{\"FDeptNumber\":\"DBM00000049\",\"FProvinceCode\":\"8\"}]}";
		// 解析json
		JSONObject jsonObject = JSONObject.parseObject(text);
		String spid = (String) jsonObject.get("FMatNumber");
		JSONArray array = (JSONArray) jsonObject.get("FEntry");	
		// 转成省区编码数组
		// 乐赛仙 
		List<Integer> lsxProvinceCodeList = new ArrayList<>();
		// 佐安堂
		List<Integer> zatProvinceCodeList = new ArrayList<>();
		// 初心
		List<Integer> cxProvinceCodeList = new ArrayList<>();

		for(int i = 0; i< array.size(); i++) {
			Integer provinceCode = Integer.parseInt((String) array.getJSONObject(i).get("FProvinceCode"));
			String deptNumber = (String) array.getJSONObject(i).get("FDeptNumber");
			if(StringUtils.equals(deptNumber, "DBM00000049")) {
				// 乐赛仙
				lsxProvinceCodeList.add(provinceCode);
			}else if(StringUtils.equals(deptNumber, "DBM00000010")) {
				// 佐安堂
				zatProvinceCodeList.add(provinceCode);
			}else {
				// 初心
				cxProvinceCodeList.add(provinceCode);
			}
		}

		Map<String, List<Integer>> map  = new HashMap<>();
		if(!lsxProvinceCodeList.isEmpty()) {
			map.put("DBM00000049", lsxProvinceCodeList);
		}
		if(!zatProvinceCodeList.isEmpty()) {
			map.put("DBM00000010", zatProvinceCodeList);
		}
		if(!cxProvinceCodeList.isEmpty()) {
			map.put("DBM00000020", cxProvinceCodeList);
		}

		//		System.out.println(map);
		for(Entry<String, List<Integer>> entry : map.entrySet()) {
			String key = entry.getKey();
			List<Integer> provinceCodeList = entry.getValue();
			StringBuilder xsqy = new StringBuilder();
			for(int i = 1; i < 35; i++) {
				if(provinceCodeList.contains(i)) {
					xsqy.append("0");
				}else {
					xsqy.append("1");
				}
			}
			System.out.println(key + "-------" + xsqy);
		}
	}*/

	/*public static void main(String[] args) {
		String text = "{\"FMatNumber\":\"SPH00000001\",\"FEntry\":[{\"FProvinceCode\":\"6\"},{\"FProvinceCode\":\"7\"},{\"FProvinceCode\":\"8\"}]}";
		// 解析json
		JSONObject jsonObject = JSONObject.parseObject(text);
		String spid = (String) jsonObject.get("FMatNumber");
		JSONArray array = (JSONArray) jsonObject.get("FEntry");
		// 转成省区编码数组
		List<Integer> provinceCodeList = new ArrayList<>();
		for(int i = 0; i< array.size(); i++) {
			String provinceCode = (String) array.getJSONObject(i).get("FProvinceCode");
			provinceCodeList.add(Integer.parseInt(provinceCode));
		}
		StringBuilder xsqy = new StringBuilder();
		for(int i = 1; i < 35; i++) {
			if(provinceCodeList.contains(i)) {
				xsqy.append("1");
			}else {
				xsqy.append("0");
			}
		}
		System.out.println(xsqy);
	}*/
	/*public static void main(String[] args) {
		String text = "{\"FCustNumber\":\"DWI00048585\",\"FEntry\":[{\"FMatNumber\":\"SPH00000009\"},{\"FMatNumber\":\"SPH00001663\"},"
				+ "{\"FMatNumber\":\"SPH00002338\"},{\"FMatNumber\":\"SPH00002549\"}]}";	
		JSONObject jsonObject = JSONObject.parseObject(text);
		String dwbh = (String) jsonObject.get("FCustNumber");

//		JSONArray array = JSONArray.parseArray(jsonObject.get("FEntry").toString());
		JSONArray array = (JSONArray) jsonObject.get("FEntry");
		String spid = (String) array.getJSONObject(0).get("FMatNumber");
		List<SpOldMchk> spOldMchkList = new ArrayList<>();
		for(int i = 0; i< array.size(); i++) {
			SpOldMchk spOldMchk = new SpOldMchk();
			spOldMchk.setDwbh(dwbh);
			spOldMchk.setSpid((String) array.getJSONObject(0).get("FMatNumber"));
			spOldMchkList.add(spOldMchk);
		}

		System.out.println(JsonUtils.objectToJson(spOldMchkList));

	}*/

}
